////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CMoneyBag.cpp
//
//		Author			:		Lane Shapiro
//
//		Purpose			:		Functions and members of the CMoneyBag class
//
//		Dates			:		Written( April 1, 2011 ) by Lane Shapiro
//	
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CMoneyBag.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../SGD Wrappers/CSGD_Direct3D.h"
#include "../Managers/VIN_ObjectManager.h"
#include "../Core/MessageSystem.h"
#include "../Managers/InputManager.h"
#include "../Managers/vin_soundManager.h"
#include "../SGD Wrappers/SGD_Math.h"
#include "CPlayer.h"
#include "../Game States/CGamePlayState.h"

CMoneyBag::CMoneyBag()
{
	inRange = false;
	isFalling = false;
	velocityTimer = 0.15f;

	value = 1;
}

CMoneyBag::~CMoneyBag()
{

}

void CMoneyBag::Render()
{
	CSGD_TextureManager* pTM = CSGD_TextureManager::GetInstance();
	pTM->Draw( GetImageID(), (int)GetPosX(), (int)GetPosY(), 1.0f, 1.0f );

	//RECT Collision;
	//SetRect( &Collision, (int)GetPosX(), (int)GetPosY(), int(GetPosX() + GetWidth()), int(GetPosY() + GetHeight()) );
}

void CMoneyBag::Update( float fElapsedTime )
{

	if(!isFalling)
	{
		velocityTimer -= fElapsedTime;

		if(velocityTimer < 0)
		{
			SetVelX(0.0f);
			SetVelY(0.0f);
			isFalling = true;
		}
	}
	else
	{
		if(GetPosY() < 480)
			SetVelY(GetVelY() - (-150.0f * fElapsedTime));
		else
			SetVelY(0.0f);
	}

	SetPosX( GetPosX() + ( GetVelX() * fElapsedTime ));
	SetPosY( GetPosY() + ( GetVelY() * fElapsedTime ));

	if(absolute(InputManager::GetInstance()->CursorPosition().x - GetPosX()) < 150 && absolute(InputManager::GetInstance()->CursorPosition().y - GetPosY()) < 150)
	{
		if(!inRange)
			inRange = true;
	}

	if(inRange)
	{
		if(InputManager::GetInstance()->CursorPosition().x > GetPosX())
		{
			SetPosX(GetPosX() + 5.0f);
		}
		else
		{
			SetPosX(GetPosX() - 5.0f);
		}

		if(InputManager::GetInstance()->CursorPosition().y > GetPosY())
		{
			SetPosY(GetPosY() + 5.0f);
		}
		else
		{
			SetPosY(GetPosY() - 5.0f);
		}
	}

	if(absolute(InputManager::GetInstance()->CursorPosition().x - GetPosX()) < 25 && absolute(InputManager::GetInstance()->CursorPosition().y - GetPosY()) < 25)
	{
		MessageSystem::GetInstance()->SendNewMessage(new CDestroyObjectMessage(this, 2));
		CGamePlayState::GetInstance()->GetPlayer()->SetCash(CGamePlayState::GetInstance()->GetPlayer()->GetCash() + GetValue());
		VIN_SoundManager::GetInstance()->PlayPickup();
		VIN_SoundManager::GetInstance()->PlayPickup();
		VIN_SoundManager::GetInstance()->PlayPickup();
		VIN_SoundManager::GetInstance()->PlayPickup();
		VIN_SoundManager::GetInstance()->PlayPickup();
	}

	if(GetPosX() < 0)
	{
		SetPosX(0);
	}
}

RECT CMoneyBag::GetRect()
{
	RECT CollisionRect;
	SetRect(&CollisionRect, (int)GetPosX(), (int)GetPosY(), (int)(GetPosX() + GetWidth()), (int)(GetPosY() + GetHeight()));
	return CollisionRect;
}

bool CMoneyBag::CheckCollision( IBaseInterface* pBase )
{
	RECT TempRect;

	if(IntersectRect(&TempRect, &GetRect(), &pBase->GetRect() ) )
	{
		if( pBase->GetType() == OBJ_PLAYER )
		{	
			// If the power up touches the Player, do the following
			CPlayer* pPBase = (CPlayer*)pBase;

			pPBase->SetCash(pPBase->GetCash() + value);

			MessageSystem::GetInstance()->SendNewMessage(new CDestroyObjectMessage(this, 2));

			return true;
		}
	}
	return false;
}